package org.linlinjava.litemall.admin.web2.user;

import com.github.pagehelper.PageHelper;
import io.swagger.annotations.*;
import org.linlinjava.litemall.admin.service2.AUserService;
import org.linlinjava.litemall.admin.web2.user.req.UReq1;
import org.linlinjava.litemall.admin.web2.user.req.UReq2;
import org.linlinjava.litemall.admin.web2.user.req.UReq3;
import org.linlinjava.litemall.admin.web2.user.req.UReq4;
import org.linlinjava.litemall.admin.web2.user.resp.*;
import org.linlinjava.litemall.core.util.R;
import org.linlinjava.litemall.core.util.Util;
import org.linlinjava.litemall.db.dao.TShopApplyMapper;
import org.linlinjava.litemall.db.dao.TShopMapper;
import org.linlinjava.litemall.db.dao.TUserInfoMapper;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.enumeration.Const;
import org.linlinjava.litemall.db.util.Str;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;


@Api("用户管理")
@RestController
@RequestMapping("/user")
public class AUserController {


    @ApiOperation(value = "下拉框-获取直营店")
    @GetMapping("/getDirectShop")
    public Object getDirectShop() {
        log.info("获取直营店");
        List<Integer> identities = Util.aList(1);
        identities.add(Const.Identity.DIRECT);
        List<UResp1> resList = userService.getShop(identities);
        return R.ok(resList);
    }

    @ApiOperation(value = "修改页面下拉框-获取直属上级")
    @GetMapping("/getDirectShop2")
    public Object getDirectShop2() {
        log.info("获取直营店2");
        List<Integer> identities = Util.aList(1);
        identities.add(Const.Identity.DIRECT);
        List<UResp1> resList = userService.getShop(identities);
        resList.add(new UResp1(0, "平台"));
        resList.sort(Comparator.comparingInt(UResp1::getId));
        return R.ok(resList);
    }

    @ApiOperation(value = "待审核管理列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "姓名", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "起始页", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页大小", paramType = "query")
    })
    @GetMapping("/applyList")
    public Object applyList(@RequestParam(required = false) String name,
                            @RequestParam(required = false) String phone,
                            @RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer size) {
        PageHelper.startPage(page, size);
        List<TShopApply> shopApplies = shopApplyMapper.selectByExample(TShopApplyExample.newAndCreateCriteria()
                .when(!Str.isEmpty(name), c -> c.andContactsNameLike(Str.toLike(name)))
                .when(!Str.isEmpty(phone), c -> c.andPhoneLike(Str.toLike(phone)))
                .example().orderBy("CREATED_TIME desc"));
        List<UResp2> list = Util.aList(shopApplies.size());
        shopApplies.forEach(a -> list.add(new UResp2(a, findUserInfoByUser2Id(a.getUserId()))));
        return R.okList(list, shopApplies);
    }

    @ApiOperation(value = "个人团长列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "姓名", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "起始页", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页大小", paramType = "query"),
            @ApiImplicitParam(name = "belongDirectId", value = "直营店id", paramType = "query"),
            @ApiImplicitParam(name = "beginDate", value = "起始时间[yyyy-MM-dd]", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束时间[yyyy-MM-dd]", paramType = "query")
    })
    @GetMapping("/distributorList")
    @ApiResponses({@ApiResponse(response = UResp3.class, message = "正常响应", code = 200)
    })
    public Object distributorList(@RequestParam(required = false) String name,
                                  @RequestParam(required = false) String phone,
                                  @RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer size,
                                  @RequestParam(required = false) String beginDate,
                                  @RequestParam(required = false) Integer belongDirectId,
                                  @RequestParam(required = false) String endDate) {
        return userService.distributorList(name, phone, page, size, beginDate, endDate, belongDirectId);
    }

    @ApiOperation(value = "提货点团长列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "姓名", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "起始页", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页大小", paramType = "query"),
            @ApiImplicitParam(name = "belongDirectId", value = "直营店id", paramType = "query"),
            @ApiImplicitParam(name = "beginDate", value = "起始时间[yyyy-MM-dd]", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束时间[yyyy-MM-dd]", paramType = "query")
    })
    @GetMapping("/selfmentionList")
    @ApiResponses({@ApiResponse(response = UResp4.class, message = "正常响应", code = 200)
    })
    public Object selfmentionList(@RequestParam(required = false) String name,
                                  @RequestParam(required = false) String phone,
                                  @RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer size,
                                  @RequestParam(required = false) String beginDate,
                                  @RequestParam(required = false) Integer belongDirectId,
                                  @RequestParam(required = false) String endDate) {
        return userService.selfmentionList(name, phone, page, size, beginDate, endDate, belongDirectId);
    }

    @ApiOperation(value = "加盟店团长列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "姓名", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "起始页", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页大小", paramType = "query"),
            @ApiImplicitParam(name = "belongDirectId", value = "直营店id", paramType = "query"),
            @ApiImplicitParam(name = "beginDate", value = "起始时间[yyyy-MM-dd]", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束时间[yyyy-MM-dd]", paramType = "query")
    })
    @GetMapping("/franshisedList")
    @ApiResponses({@ApiResponse(response = UResp5.class, message = "正常响应", code = 200)
    })
    public Object franshisedList(@RequestParam(required = false) String name,
                                 @RequestParam(required = false) String phone,
                                 @RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer size,
                                 @RequestParam(required = false) String beginDate,
                                 @RequestParam(required = false) Integer belongDirectId,
                                 @RequestParam(required = false) String endDate) {
        return userService.franshisedList(name, phone, page, size, beginDate, endDate, Const.Identity.FRANSHISED, belongDirectId);
    }

    @ApiOperation(value = "特殊加盟店团长列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "姓名", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "起始页", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页大小", paramType = "query"),
            @ApiImplicitParam(name = "belongDirectId", value = "直营店id", paramType = "query"),
            @ApiImplicitParam(name = "beginDate", value = "起始时间[yyyy-MM-dd]", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束时间[yyyy-MM-dd]", paramType = "query")
    })
    @GetMapping("/franshised2List")
    @ApiResponses({@ApiResponse(response = UResp5.class, message = "正常响应", code = 200)
    })
    public Object franshised2List(@RequestParam(required = false) String name,
                                  @RequestParam(required = false) String phone,
                                  @RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer size,
                                  @RequestParam(required = false) String beginDate,
                                  @RequestParam(required = false) Integer belongDirectId,
                                  @RequestParam(required = false) String endDate) {
        return userService.franshisedList(name, phone, page, size, beginDate, endDate, Const.Identity.FRANSHISED2, belongDirectId);
    }

    @ApiOperation(value = "用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "姓名", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "起始页", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页大小", paramType = "query"),
            @ApiImplicitParam(name = "belongDirectId", value = "直营店id", paramType = "query"),
    })
    @GetMapping("/allUserList")
    @ApiResponses({@ApiResponse(response = UResp6.class, message = "正常响应", code = 200)
    })
    public Object allUserList(@RequestParam(required = false) String name,
                              @RequestParam(required = false) String phone,
                              @RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer size,
                              @RequestParam(required = false) Integer belongDirectId) {
        return userService.allUserList(name, phone, page, size, belongDirectId);
    }

    @ApiOperation(value = "直营店列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "belongDirectId", value = "直营店id", paramType = "query"),
    })
    @GetMapping("/directDataList")
    @ApiResponses({@ApiResponse(response = UResp9.class, message = "正常响应", code = 200)
    })
    public Object directDataList(@RequestParam(required = false) Integer belongDirectId) {
        return R.fail("接口已失效");
//        return userService.directDataList(belongDirectId);
    }

    @ApiOperation(value = "特殊加盟店团长列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "姓名", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "起始页", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页大小", paramType = "query"),
            @ApiImplicitParam(name = "belongDirectId", value = "直营店id", paramType = "query"),
            @ApiImplicitParam(name = "beginDate", value = "起始时间[yyyy-MM-dd]", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束时间[yyyy-MM-dd]", paramType = "query")
    })
    @GetMapping("/directDataList2")
    @ApiResponses({@ApiResponse(response = UResp5.class, message = "正常响应", code = 200)
    })
    public Object directDataList2(@RequestParam(required = false) String name,
                                  @RequestParam(required = false) String phone,
                                  @RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer size,
                                  @RequestParam(required = false) String beginDate,
                                  @RequestParam(required = false) Integer belongDirectId,
                                  @RequestParam(required = false) String endDate) {
        return userService.franshisedList(name, phone, page, size, beginDate, endDate, Const.Identity.DIRECT, belongDirectId);
    }

    @ApiOperation(value = "提现管理列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "transNo", value = "单号", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "起始页", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页大小", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态[0-未审核、1-已审核、2-审核失败", paramType = "query"),
            @ApiImplicitParam(name = "beginDate", value = "起始时间[yyyy-MM-dd]", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束时间[yyyy-MM-dd]", paramType = "query")
    })
    @GetMapping("/cashOutList")
    @ApiResponses({@ApiResponse(response = UResp7.class, message = "正常响应", code = 200)
    })
    public Object cashOutList(@RequestParam(required = false) String transNo,
                              @RequestParam(required = false) String phone,
                              @RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer size,
                              @RequestParam(required = false) String beginDate,
                              @RequestParam(required = false) Integer status,
                              @RequestParam(required = false) String endDate) {
        log.info("提现管理列表");
        return userService.cashOutList(transNo, phone, page, size, beginDate, endDate, status);
    }


    @ApiOperation(value = "审核管理-提交")
    @PostMapping("/saveApply")
    public Object saveApply(@RequestBody UReq1 req) {
        return userService.saveApply(req);
    }

    @ApiOperation(value = "提现管理-提交")
    @PostMapping("/saveWxCashOutApply")
    public Object saveWxCashOutApply(@RequestBody UReq2 req) {
        return userService.saveWxCashOutApply(req);
    }

    @ApiOperation(value = "查看用户修改资料")
    @GetMapping("/get")
    @ApiResponses({@ApiResponse(response = UResp8.class, message = "正常响应", code = 200)
    })
    public Object get(@RequestParam Integer id) {
        log.info("查看用户修改资料");
        Object o = userService.get(id);
        return o;
    }

    @ApiOperation(value = "删除团长", notes = "只传id")
    @PostMapping("/del")
    public Object del(@RequestBody UReq4 req) {
        log.info("删除团长，参数[id = {}]", req.getId());
        Object o = userService.del(req);
        return o;
    }


    @ApiOperation(value = "用户修改资料-保存", notes = "传什么字段就只能保存什么字段")
    @PostMapping("/save")
    public Object save(@RequestBody UReq3 req) {
        log.info("用户修改资料-保存");
        Object o = userService.save(req);
        return o;
    }


    private TUserInfo findUserInfoByUser2Id(Integer userId) {
        return userInfoMapper.selectOneByExample(TUserInfoExample.newAndCreateCriteria().andUser2IdEqualTo(userId).example());
    }


    private static Logger log = LoggerFactory.getLogger(AUserController.class);
    @Autowired
    private TShopApplyMapper shopApplyMapper;
    @Autowired
    private TUserInfoMapper userInfoMapper;
    @Autowired
    private AUserService userService;


}


